<?php
namespace app\pay\controller\yop;

use think\Controller;
use pay\yop\Config;

use pay\yop\lib\YopRequest;
use pay\yop\lib\YopClient3;
use pay\yop\lib\Util\YopSignUtils;

class Testorder extends Controller {
    private $cfg = null;
    
    public function _initialize() {
        
        
        $this->cfg = new Config();
    }

    public function order() {
        date_default_timezone_set('Asia/Shanghai');
        $orderId = 'CY' . date('YmdHis', time()) . rand(10, 99);
        $this->assign('timestamp', time());
        $this->assign('orderId', $orderId);
        $this->assign('dateTime', date('YmdHis', time()));
        return $this->fetch();
    }
    
    public function getString($response){

        $str="";

        foreach ($response as $key => $value) {
            $str .= $key . "=" . $value . "&";
        }
        $getSign = substr($str, 0, strlen($str) - 1);
            return $getSign;
    }
 
    public function getUrl($response,$private_key)
        {
           $content = $this->getString($response);
           $sign=YopSignUtils::signRsa($content,$private_key);
           $url=$content."&sign=".$sign;
           return $url;
        }

    public function object_array($array) { 
        if(is_object($array)) { 
            $array = (array)$array; 
         } if(is_array($array)) { 
             foreach($array as $key=>$value) { 
                 $array[$key] = $this->object_array($value); 
                 } 
         } 
         return $array; 
    }

    public function sendOrder() {
        $data = input('post.');
        $request = new YopRequest("OPR:" . $this->cfg->C('merchantId'), $this->cfg->C('private_key'), $this->cfg->C('orderUrl'), $this->cfg->C('yop_public_key'));
        $request->addParam("parentMerchantNo", $this->cfg->C('parentMerchantId'));
        $request->addParam("merchantNo", $this->cfg->C('merchantId'));
        $request->addParam("orderId", $data['orderId']);
        $request->addParam("orderAmount", $data['orderAmount']);
        $request->addParam("timeoutExpress", $data['timeoutExpress']);
        $request->addParam("requestDate", $data['requestDate']);
        $request->addParam("redirectUrl", $data['redirectUrl']);
        $request->addParam("notifyUrl", $data['notifyUrl']);
        $request->addParam("goodsParamExt", $data['goodsParamExt']);
        $request->addParam("paymentParamExt", $data['paymentParamExt']);
        $request->addParam("industryParamExt", $data['industryParamExt']);
        $request->addParam("memo", $data['memo']);
        $request->addParam("riskParamExt", $data['riskParamExt']);
        $request->addParam("csUrl", $data['csUrl']);
        $request->addParam("fundProcessType", $data['fundProcessType']);
	$request->addParam("divideDetail", $data['divideDetail']);
	$request->addParam("divideNotifyUrl", $data['divideNotifyUrl']);
        $response = YopClient3::post("/rest/v1.0/sys/trade/order", $request);
	 //     var_dump($response);
        if($response->validSign == 1){
             echo "返回结果签名验证成功!\n";
        } else {
            echo "返回结果签名验证失败!\n";
        }
        //取得返回结果
        $resData = $this->object_array($response);

       // echo  $resData;
        $token = $resData['result']['token'];
        $cashter = array(
            "merchantNo"    => $this->cfg->C('merchantId') ,
            "token"         => $token,
            "timestamp"     => $data['timestamp'],
            "directPayType" => $data['directPayType'],
            "cardType"      => $data['cardType'],
            "userNo"        => $data['userNo'],
            "userType"      => $data['userType'],
            "ext"           => $data['ext'],
        ); 

        $getUrl = $this->getUrl($cashter, $this->cfg->C('private_key'));   
        $newUrl = str_replace("&timestamp", "&amp;timestamp", $getUrl);
        //print_r($newUrl);
        $url = "https://cash.yeepay.com/cashier/std?" . $newUrl;
        return $url ;
    } 
}

